package com.mengfou;


import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.function.ToIntFunction;
import java.util.stream.IntStream;

/**
 * @author 梦否
 * @date 2025/06/28 22:19
 */
public class JavaAssistLearn {

    public static void main(String[] args) {
        JavaAssistLearn.MyClassLoader myClassLoader = new JavaAssistLearn.MyClassLoader(JavaAssistLearn.class.getClassLoader());
        LogProxy logProxy = new LogProxy(myClassLoader);
        logProxy.error("Hello", "JavaAssist1");
        logProxy.hotFix(true);
        logProxy.error("Hello", "JavaAssist2");
        logProxy.printMessageSize();


        List<String> strings = Arrays.asList("1", "2", "3", "4", "5");
        IntStream intStream = strings.stream().filter(s -> false).mapToInt(Integer::parseInt);
        int[] ints = intStream.toArray();
        for (int anInt : ints) {
            System.out.println(anInt);
        }
    }

    public static class MyClassLoader extends ClassLoader {

        private String path = "D:\\My Proj\\testForLearn\\test-target\\classes";

        private ClassLoader originalClassLoader;
        protected MyClassLoader(ClassLoader parent) {
            originalClassLoader = parent;
        }

        @Override
        public Class<?> loadClass(String name) throws ClassNotFoundException {
            try {
                return originalClassLoader.loadClass(name);
            } catch (ClassNotFoundException e) {
                System.out.println("find from custom classloader.");
                String replace = name.replace(".", "\\\\");
                File file = new File(path, replace+".class");
                if(file.exists()) {
                    byte[] data = FileUtils.loadByteArray(file);
                    return defineClass(null, data, 0, data.length);
                }
                throw new ClassNotFoundException("file not exist. path: " + file.getAbsolutePath());
            }
        }
    }
}
